package com.ouc.dataMange.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.ouc.dataMange.domain.SysDiskSpace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ouc.dataMange.mapper.SysDataSetMapper;
import com.ouc.dataMange.domain.SysDataSet;
import com.ouc.dataMange.service.ISysDataSetService;

import javax.swing.filechooser.FileSystemView;

/**
 * dataSetService业务层处理
 *
 * @author ouc
 * @date 2025-07-14
 */
@Service
public class SysDataSetServiceImpl implements ISysDataSetService {
  @Autowired
  private SysDataSetMapper sysDataSetMapper;

  /**
   * 查询dataSet
   *
   * @param id dataSet主键
   * @return dataSet
   */
  @Override
  public SysDataSet selectSysDataSetById(Integer id) {
    return sysDataSetMapper.selectSysDataSetById(id);
  }

  /**
   * 查询dataSet列表
   *
   * @param sysDataSet dataSet
   * @return dataSet
   */
  @Override
  public List<SysDataSet> selectSysDataSetList(SysDataSet sysDataSet) {
    return sysDataSetMapper.selectSysDataSetList(sysDataSet);
  }

  /**
   * 新增dataSet
   *
   * @param sysDataSet dataSet
   * @return 结果
   */
  @Override
  public int insertSysDataSet(SysDataSet sysDataSet) {
    return sysDataSetMapper.insertSysDataSet(sysDataSet);
  }

  /**
   * 修改dataSet
   *
   * @param sysDataSet dataSet
   * @return 结果
   */
  @Override
  public int updateSysDataSet(SysDataSet sysDataSet) {
    return sysDataSetMapper.updateSysDataSet(sysDataSet);
  }

  /**
   * 批量删除dataSet
   *
   * @param ids 需要删除的dataSet主键
   * @return 结果
   */
  @Override
  public int deleteSysDataSetByIds(Long[] ids) {
    return sysDataSetMapper.deleteSysDataSetByIds(ids);
  }

  /**
   * 删除dataSet信息
   *
   * @param id dataSet主键
   * @return 结果
   */
  @Override
  public int deleteSysDataSetById(Long id) {
    return sysDataSetMapper.deleteSysDataSetById(id);
  }

  /**
   * 获取磁盘目录及剩余空间
   *
   * @return
   */
  @Override
  public List<SysDiskSpace> getFileSystemView() {
    FileSystemView fsv = FileSystemView.getFileSystemView();
    File[] fs = File.listRoots();
    List<SysDiskSpace> spaceList = new ArrayList<>();
    for (int i = 0; i < fs.length; i++) {
//      String systemDisplayName = fsv.getSystemDisplayName(fs[i]);
//      long totalSpace = fs[i].getTotalSpace();
//      long freeSpace = fs[i].getFreeSpace();
//      long usableSpace = fs[i].getUsableSpace();
      SysDiskSpace sysDiskSpace = new SysDiskSpace();
      sysDiskSpace.setDiskName(fsv.getSystemDisplayName(fs[i]));
      String[] split = fs[i].getPath().split(":");
      sysDiskSpace.setMemoryDevice(split[0]+":");
      sysDiskSpace.setTotalSpace(fs[i].getTotalSpace() / (1024 * 1024 * 1024));
      sysDiskSpace.setFreeSpace(fs[i].getFreeSpace() / (1024 * 1024 * 1024));
      sysDiskSpace.setUseSpace((fs[i].getTotalSpace() - fs[i].getUsableSpace()) / (1024 * 1024 * 1024));
      spaceList.add(sysDiskSpace);
    }
    return spaceList;
  }
}
